/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     data.h
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-01
  * @brief    
 ********************************************************************************/

#ifndef _DATA_H
#define _DATA_H

#include <stdint.h>
#include "module_select.h"
#include "delay.h"
#include "ltprintf.h"
#include "lt168.h"

// tft info
extern uint32_t gBaudrate;
extern uint16_t LCD_XSIZE_TFT; // screen resolution
extern uint16_t LCD_YSIZE_TFT;
extern uint16_t LCD_VBPD;
extern uint16_t LCD_VFPD;
extern uint16_t LCD_VSPW;
extern uint16_t LCD_HBPD;
extern uint16_t LCD_HFPD;
extern uint16_t LCD_HSPW;
extern uint8_t gPCLKRISING;
extern uint8_t gHSYNCPolarity;
extern uint8_t gVSYNCPolarity;
extern uint8_t gDEPolarity;
extern uint8_t Screen_DIR;
extern uint32_t TFT_color;

// pic
#define DMA_LEN1 1024
#define DMA_LEN2 2048
#define DMA_LEN3 3072
#define DMA_LEN4 4096
extern uint8_t DMA_rxbuf1[DMA_LEN4];	//for dma buffer
extern uint8_t DMA_rxbuf2[DMA_LEN4];
extern uint8_t DMA_txbuf1[2048];
extern uint8_t Table_color[512];
#define MAX_PixelSize  204800       //320*320*2 = 204800
extern uint8_t m_PngData[MAX_PixelSize];
extern uint8_t *dispaly_ram;       // 480*320*2 = 307200
extern uint8_t *mix_ram;  // ����ָ��

extern uint16_t *p16_displayram;
extern uint16_t *p16_m_PngData;
extern uint16_t *p16_DMA_rxbuf1;
extern uint16_t *p16_DMA_rxbuf2;

extern uint16_t FullScreen_W, FullScreen_H;	//for full-screen info
extern uint32_t FullScreen_addr, FullScreen_color;
extern uint8_t FullScreen_flag;

extern uint16_t Page_VirW, Page_VirH;
extern uint32_t Page_VirAddr, Page_VirLen;
extern uint8_t Page_VirFlag;

/* ------ picture parameter ------ */
typedef struct
{
	uint16_t w;
	uint16_t h;
	uint8_t depth; // Picture color depth
	uint8_t Cflag; // Picture type
	//	uint8_t  Table_color[512];
} zippic_struct;

extern zippic_struct zippic;

typedef struct
{
	uint16_t buff_x;
	uint16_t buff_y;
	uint16_t buff_w;
	uint16_t cutx;
	uint16_t cuty;
	uint16_t cutw;
	uint16_t cuth;
	uint16_t cut_flag;      //   ==0  ����jpg����0��ʼ���buffer, 
							//   ==1  ����/����jpg�����뵽ָ��bufferλ��
}jpg_TRAN;

extern jpg_TRAN jpgTr;

typedef struct
{
	uint32_t addr;
	uint16_t w;
	uint16_t h;
	uint32_t len; // picture size
	uint8_t flag; // picture type, 0x10:pure color
	uint16_t x;
	uint16_t y;
//	uint8_t mix;  // mix mode, 0:with fullscreen interface   1:with specific pictures
} _PICINFO;

extern _PICINFO PICINFO[];

typedef enum
{
    ColorRGB565 = 0,
    ColorARGB0565,     // soft png
    ColorARGB4444,
    ColorRGB565_zip,   // 3 -- zip
    ColorARGB0565_zip,
    ColorARGB4444_zip,
    ColorARGB8888,     // 6 -- 3688
    ColorJPG,          // 7 -- unuse
    ColorARGB8565,     // 8 
    ColorZoomin,       // 9 -- unuse
    ColorPure = 0x10,  // 0x10 -- pure color
}lt_pic_format;

typedef enum
{
    PIC_ON_BaseImg   = 0,     // on fullscreen background
    PIC_ON_LCDImg    = 1,      // on tftlcd screen 
	PIC_ON_PureColor = 2,      // on pure color

    //PIC_ON_GifImg  = 2, 
    //PIC_ON_IconImg = 3, 
    //PIC_ON_FONTImg,         // is num/font/text
    //PIC_ON_EncoderImg,      // is encoder ICON
}lt_pic_mix_type;

typedef enum
{
	PIC_TO_SCREEN = 0,    // pic to screen
    PIC_TO_BUFF ,         // pic to special buffer
    PIC_TO_FULLBUFF,      // pic to full-screen buffer
    PIC_TO_FULLBUFF_FIRST,// pic to full-screen buffer
}lt_pic_to_flag;

typedef enum
{
	DIS_THE_FIRST = 0,   //  
	DIS_THE_SECOND ,     //  
}lt_dis_flag;

typedef struct
{
	uint8_t *p;       // displayed start address
	uint16_t x;       // displayed area
	uint16_t y; 
	uint16_t w;
	uint16_t h;
	uint32_t pos;
	uint8_t to_flag;   // lt_pic_to_flag
	uint8_t dis_flag;  // lt_dis_flag
} _DISPLAY_INFO;
extern _DISPLAY_INFO diplay_info;

/* ------ picture parameter ------ */

/* encoder */
typedef enum
{
    ENCODER_SINGLE_CLICK = 0,
    ENCODER_DOUBLE_CLICK,
    ENCODER_LONG_PRESS,
    ENCODER_ROTATE_LEFT = 0x0A,
    ENCODER_ROTATE_RIGHT,
}lt_encoder_action;

/* backlight */
extern uint16_t gControlDuty;

//---------------Macro used to control USART receiving data-----------------------------
#define RX_SIZE ((2048 + 20)*2) //(4096 + 2)

typedef struct
{
	uint8_t Flag;
	uint8_t Buf[RX_SIZE];
	uint16_t Count;
} USART_RX_INFO;
extern USART_RX_INFO gUsartRx;

//--------------------- TP ----------------------------------
extern uint8_t First_press;
extern uint16_t First_pressX, First_pressY;
extern uint8_t RTP_read_Flag;
extern uint16_t RTP_time;

#define CT_MAX_TOUCH 5 // Number of points supported by capacitive screen

typedef struct
{
	uint8_t (*scan)(void);	  // Scan the touch screen;
	uint8_t sta;			  // Status flag,  0:Not pressed	1:First press	2:Keep pressing
	uint16_t x[CT_MAX_TOUCH]; // Screen coordinates
	uint16_t y[CT_MAX_TOUCH]; // Screen coordinates
	uint16_t px;			  // Physical coordinates
	uint16_t py;			  // Physical coordinates
	uint8_t readspace;		  // Coordinate reading interval

	uint8_t flag; // Read from flash. When the value is 0x5a, it means that there are four parameters in flash
	float xfac;
	float yfac;
	float xoff;
	float yoff;
} TP_INFO;

extern TP_INFO gTpInfo;

typedef union
{
	float xfac;
	int8_t s[4];
} TP_xfac;

typedef union
{
	float xoff;
	int8_t s[4];
} TP_xoff;

typedef union
{
	float yfac;
	int8_t s[4];
} TP_yfac;

typedef union
{
	float yoff;
	int8_t s[4];
} TP_yoff;

extern TP_xfac gTpxfac;
extern TP_xoff gTpxoff;
extern TP_yfac gTpyfac;
extern TP_yoff gTpyoff;

//lcd
extern uint8_t lcdtft_TE_flag;

#endif /* _DATA_H */
